﻿Public Class parser

    Public Shared Function doParse(ByVal str As String) As Boolean

        str = str.ToLower

        'CYCLE
        If (str.Contains("cycle")) Then
            Dim cycle_str_begin As Integer = str.IndexOf("cycle") + 6
            Dim cycle_str_end As Integer = str.IndexOf(")", cycle_str_begin)
            Dim str_cycle_val As String = str.Substring(cycle_str_begin, cycle_str_end - cycle_str_begin - 1)
            Dim cycle_val As Integer = CType(str_cycle_val, Integer)
            WM.Cycle = cycle_val
        End If

        'MAP
        If (str.Contains("map")) Then
            Dim map_str_begin As Integer = str.IndexOf("map") + 3
            Dim map_str_param_begin As Integer = str.IndexOf("(", map_str_begin) + 1
            Dim map_str_param_end As Integer = str.IndexOf(")", map_str_param_begin)
            Dim str_map_params As String = str.Substring(map_str_param_begin, map_str_param_end - map_str_param_begin)
            Dim vec_map_str_params As String() = str_map_params.Trim.Split(" ")
            Dim map_x As Double = vec_map_str_params(0)
            Dim map_y As Double = vec_map_str_params(1)
            WM.mapW = map_x
            WM.mapH = map_y
        End If

        'SELF
        If (str.Contains("self")) Then
            Dim self_str_begin As Integer = str.IndexOf("self") + 4
            Dim self_str_param1_begin As Integer = str.IndexOf("(", self_str_begin) + 1
            Dim self_str_param1_end As Integer = str.IndexOf(")", self_str_begin)
            Dim str_self_param1 As String = str.Substring(self_str_param1_begin, self_str_param1_end - self_str_param1_begin - 1)
            Dim vec_self_param1 As String() = str_self_param1.Trim.Split(" ")
            Dim self_x As Double = vec_self_param1(0)
            Dim self_y As Double = vec_self_param1(1)
            Dim self_z As Double = vec_self_param1(2)
            WM.MyPos = New vector(self_z, self_x, self_y)

            Dim self_str_param2_begin As Integer = str.IndexOf("(", self_str_param1_end) + 1
            Dim self_str_param2_end As Integer = str.IndexOf(")", self_str_param2_begin)
            Dim str_self_param2 As String = str.Substring(self_str_param2_begin, self_str_param2_end - self_str_param2_begin - 1)
            Dim vec_self_param2 As String() = str_self_param2.Trim.Split(" ")
            Dim self_theta_x As Double = vec_self_param2(0)
            Dim self_theta_y As Double = vec_self_param2(1)
            Dim self_theta_z As Double = vec_self_param2(2)
            WM.MyAngle = otherMath.degNormalize((otherMath.RadToDeg(Math.Atan2(self_theta_z, self_theta_x)) - 75) * -1)

            Dim self_str_param3_begin As Integer = str.IndexOf("(", self_str_param2_end) + 1
            Dim self_str_param3_end As Integer = str.IndexOf(")", self_str_param3_begin)
            Dim str_self_param3 As String = str.Substring(self_str_param3_begin, self_str_param3_end - self_str_param3_begin - 1)
            Dim self_health As Double = CType(str_self_param3, Double)
            WM.Health = self_health
        End If

        'OPPONENTS
        If (str.Contains("opponents")) Then
            Dim po As PlayerObject() = New PlayerObject(-1) {}
            Dim opp_str_begin As Integer = str.IndexOf("opponents") + 9
            While (str.IndexOf("(", opp_str_begin) < str.IndexOf(")", opp_str_begin))
                Array.Resize(po, po.Length + 1)
                Dim opp_str_param1_begin As Integer = str.IndexOf("(", opp_str_begin) + 1
                Dim opp_str_param1_end As Integer = str.IndexOf(")", opp_str_begin)
                Dim str_opp_param1 As String = str.Substring(opp_str_param1_begin, opp_str_param1_end - opp_str_param1_begin - 1)
                Dim vec_opp_param1 As String() = str_opp_param1.Trim.Split(" ")
                Dim opp_x As Double = vec_opp_param1(0)
                Dim opp_y As Double = vec_opp_param1(1)
                Dim opp_z As Double = vec_opp_param1(2)
                po(po.Length - 1).pos = New vector(opp_z, opp_x, opp_y)
                Dim opp_str_param2_begin As Integer = str.IndexOf("(", opp_str_param1_end) + 1
                Dim opp_str_param2_end As Integer = str.IndexOf(")", opp_str_param2_begin)
                Dim str_opp_param2 As String = str.Substring(opp_str_param2_begin, opp_str_param2_end - opp_str_param2_begin - 1)
                Dim vec_opp_param2 As String() = str_opp_param2.Trim.Split(" ")
                Dim opp_theta_x As Double = vec_opp_param2(0)
                Dim opp_theta_y As Double = vec_opp_param2(1)
                Dim opp_theta_z As Double = vec_opp_param2(2)
                po(po.Length - 1).theta = otherMath.degNormalize((otherMath.RadToDeg(Math.Atan2(opp_theta_z, opp_theta_x)) - 75) * -1)
                opp_str_begin = opp_str_param2_end + 1
            End While
            WM.Opponents = po
        End If

        'TEAMMATES
        If (str.Contains("teammates")) Then
            Dim po As PlayerObject() = New PlayerObject(-1) {}
            Dim mate_str_begin As Integer = str.IndexOf("teammates") + 9
            While (str.IndexOf("(", mate_str_begin) < str.IndexOf(")", mate_str_begin))
                Array.Resize(po, po.Length + 1)
                Dim mate_str_param1_begin As Integer = str.IndexOf("(", mate_str_begin) + 1
                Dim mate_str_param1_end As Integer = str.IndexOf(")", mate_str_begin)
                Dim str_mate_param1 As String = str.Substring(mate_str_param1_begin, mate_str_param1_end - mate_str_param1_begin - 1)
                Dim vec_mate_param1 As String() = str_mate_param1.Trim.Split(" ")
                Dim mate_x As Double = vec_mate_param1(0)
                Dim mate_y As Double = vec_mate_param1(1)
                Dim mate_z As Double = vec_mate_param1(2)
                po(po.Length - 1).pos = New vector(mate_z, mate_x, mate_y)
                Dim mate_str_param2_begin As Integer = str.IndexOf("(", mate_str_param1_end) + 1
                Dim mate_str_param2_end As Integer = str.IndexOf(")", mate_str_param2_begin)
                Dim str_mate_param2 As String = str.Substring(mate_str_param2_begin, mate_str_param2_end - mate_str_param2_begin - 1)
                Dim vec_mate_param2 As String() = str_mate_param2.Trim.Split(" ")
                Dim mate_theta_x As Double = vec_mate_param2(0)
                Dim mate_theta_y As Double = vec_mate_param2(1)
                Dim mate_theta_z As Double = vec_mate_param2(2)
                po(po.Length - 1).theta = otherMath.degNormalize((otherMath.RadToDeg(Math.Atan2(mate_theta_z, mate_theta_x)) - 75) * -1)
                mate_str_begin = mate_str_param2_end + 1
            End While
            WM.Teammates = po
        End If

        'Block
        Dim p As vector() = New vector(-1) {}
        Dim block_str_begin As Integer = str.IndexOf("block")
        Dim block_str_param_begin As Integer = str.IndexOf("(", block_str_begin) + 1
        Dim block_str_param_end As Integer = str.IndexOf(")", block_str_begin)
        While (str.IndexOf("block", block_str_param_begin))
            Array.Resize(p, p.Length + 1)
            Dim block_mate_param As String = str.Substring(block_str_param_begin, block_str_param_end - block_str_param_begin - 1)
            Dim vec_block_param As String() = block_mate_param.Trim.Split(" ")
            Dim block_x As Double = vec_block_param(1)
            Dim block_y As Double = vec_block_param(0)
            Dim block_z As Double = vec_block_param(2)
            p(p.Length - 1) = New vector(block_x, block_y, block_z)
            block_str_begin = str.IndexOf("block", block_str_param_end)
            If block_str_begin = -1 Then Exit While
            block_str_param_begin = str.IndexOf("(", block_str_begin) + 1
            block_str_param_end = str.IndexOf(")", block_str_param_begin)
        End While
        WM.Blocks = p

        'FIRE
        If (str.Contains("fire")) Then
            Dim fire_str_begin As Integer = str.IndexOf("fire") + 4
            Dim fire_str_end As Integer = str.IndexOf(")", fire_str_begin)
            Dim str_fire_val As String = str.Substring(fire_str_begin, fire_str_end - fire_str_begin - 1)
            Dim fire_val As Integer = CType(str_fire_val, Double)
            WM.FireCount = fire_val
        End If

        'GRANER
        If (str.Contains("graner")) Then
            Dim graner_str_begin As Integer = str.IndexOf("graner") + 6
            Dim graner_str_end As Integer = str.IndexOf(")", graner_str_begin)
            Dim str_graner_val As String = str.Substring(graner_str_begin, graner_str_end - graner_str_begin - 1)
            Dim graner_val As Integer = CType(str_graner_val, Double)
            WM.granerCount = graner_val
        End If

        'HEAR
        If (str.Contains("hear")) Then
            Dim hear_str_begin As Integer = str.IndexOf("hear") + 4
            Dim hear_str_end As Integer = str.IndexOf(")", hear_str_begin)
            Dim str_hear_val As String = str.Substring(hear_str_begin, hear_str_end - hear_str_begin - 1)
            WM.HearMessage = str_hear_val.Trim
        End If

        Return True

    End Function

End Class